﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Mvc;
using CORE;
using KM.BUS;
using KM.ENT;
using KM.Models;


namespace KM.Areas.KM.Controllers
{
    public class KhuyenMaiController : Controller
    {
        //
        // GET: /KM/KhuyenMai/
        int pageSize = 18;
        public ActionResult Index()
        {
            return View();
        }
        public PartialViewResult KhuyenMaiHot(string thanhpho)
        {
            busKhuyenMai busKM = new busKhuyenMai();
            List<entKhuyenMai> lst = new List<entKhuyenMai>();
            PagingInfoModel page = new PagingInfoModel()
            {
                CurrentPage = Utils.getIntQuery("page") == 0 ? 1 : Utils.getIntQuery("page"),
                recordPerPage = pageSize
            };
            int id = Utils.GetIdThanhPho(thanhpho);
            ViewBag.me = id;
            lst = busKM.getKhuyenMaiHot(id, ref page);
            return PartialView(lst);
        }
        public ActionResult GetKhuyenMaiByLoaiKhuyenMai(string thanhpho, string id = "")
        {
            int id2 = 0;
            //lay ten loai khuyen mai
            entLoaiKhuyenMai entLKM = new entLoaiKhuyenMai();
            busLoaiKhuyenMai busLKM = new busLoaiKhuyenMai();
            entLKM = busLKM.getInfo(id);

            id2 = entLKM.LoaiKhuyenMaiId;
            int price = 0;
            price = Utils.getIntQuery("price");
            string q = "";
            q = Utils.getStringQuery("q");

            //set cookies cho thanh pho
            if (Utils.getIntQuery("LSP") != 0)
            {
                Response.Cookies["thanhpho"].Value = Utils.getIntQuery("LSP").ToString();
                Response.Cookies["thanhpho"].Expires = DateTime.Now.AddDays(1d);
                thanhpho = Utils.GetTenThanhPho(Utils.getIntQuery("LSP"));
                return RedirectToAction("GetKhuyenMaiByLoaiKhuyenMai", new { thanhpho = thanhpho, id = id });
            }
            else
            {
                Response.Cookies["thanhpho"].Value = Utils.GetIdThanhPho(thanhpho).ToString();
            }

            busKhuyenMai busKM = new busKhuyenMai();
            List<entKhuyenMai> lst = new List<entKhuyenMai>();
            PagingInfoModel page = new PagingInfoModel()
            {
                CurrentPage = Utils.getIntQuery("page") == 0 ? 1 : Utils.getIntQuery("page"),
                recordPerPage = pageSize
            };
            lst = busKM.getAllByLoaiKhuyenMai(id2, Utils.GetIdThanhPho(thanhpho), price, q, ref page);
            ViewBag.page = page;
            ViewBag.Id = id;
            ViewBag.q = q;
            ViewBag.price = price.ToString();
            ViewBag.Tieude = entLKM.TenLoaiKhuyenMai;
            if (entLKM.Title != "")
            {
                ViewBag.Title = entLKM.Title;
                ViewBag.Description = entLKM.Description;
                ViewBag.Keywords = entLKM.Keywords;
                
            }
            else
            {
                ViewBag.Title = Constants.TITLE;
                ViewBag.Description = Constants.DESCRIPTION;
                ViewBag.Keywords = Constants.KEYWORDS ;
            }
            return View(lst);
        }

        public ActionResult ListKhuyenMai(List<entKhuyenMai> lst)
        {
            return View(lst);
        }

        public PartialViewResult ViewSearch()
        {
            ViewBag.price = "pricev=" + Utils.getIntQuery("price").ToString();
            binTP(Utils.getIntCookies("thanhpho"));
            ViewBag.q = Utils.getStringQuery("q").ToString();
            return PartialView();
        }
        #region ===bin thanh pho


        void binTP(int check)
        {
            List<entThanhPho> lst = new List<entThanhPho>();
            busThanhPho busTP = new busThanhPho();
            lst = busTP.getAllThanhPho();
            List<SelectListItem> items = new List<SelectListItem>();

            foreach (var item in lst)
            {
                items.Add(new SelectListItem()
                {
                    Value = item.ThanhPhoId.ToString(),
                    Text = item.TenThanhPho,
                    Selected = (item.ThanhPhoId == check ? true : false)
                });
            }
            ViewBag.lstThanhPho = items;
        }
        #endregion
        public PartialViewResult GetMyKhuyenMai(string thanhpho)
        {
            busKhuyenMai busKM = new busKhuyenMai();
            List<entKhuyenMai> lst = new List<entKhuyenMai>();

            int id = Utils.GetIdThanhPho(thanhpho);
            ViewBag.me = id;
            lst = busKM.getMyKhuyenMai(Constants.KM1);
            return PartialView(lst);
        }
    }
}
